package com.danger.Activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.danger.Model.bean.ObjectInfo;
import com.danger.Presenter.FoundPresenter;
import com.danger.Presenter.FoundPresenterImpl;
import com.danger.View.ObjectInfoView;
import com.danger.View.adapter.MyAdapter;

import java.util.List;

public class FoundActivity extends AppCompatActivity implements ObjectInfoView, View.OnClickListener {

    private Toolbar toolbar;
    private FloatingActionButton found_menu_fab;
    private FloatingActionButton found_search_fab;
    private FloatingActionButton found_main_fab;
    private ListView found_listView;
    private ProgressDialog dialog;
    private FoundPresenter presenter;
    private SwipeRefreshLayout found_swipeRefresh;
    private List<ObjectInfo> data;
    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_found);
        initView();
        initEvent();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.found_toolbar);
        toolbar.setTitle("招领广场");
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        found_swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.found_swipeRefresh);
        found_swipeRefresh.setColorSchemeResources(R.color.swipe_color_1, R.color.swipe_color_2, R.color.swipe_color_3, R.color.swipe_color_4);
        found_swipeRefresh.setSize(SwipeRefreshLayout.LARGE);
        found_swipeRefresh.setProgressBackgroundColor(R.color.swipe_background_color);
        found_menu_fab = (FloatingActionButton) findViewById(R.id.found_menu_fab);
        found_search_fab = (FloatingActionButton) findViewById(R.id.found_search_fab);
        found_main_fab = (FloatingActionButton) findViewById(R.id.found_main_fab);
        found_listView = (ListView) findViewById(R.id.found_listView);
        dialog = new ProgressDialog(this);
        dialog.setMessage("正在获取数据...");
        presenter = new FoundPresenterImpl(this, this);
        presenter.queryFound();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        found_swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.refersh();
                found_swipeRefresh.isRefreshing();
            }
        });
        found_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(FoundActivity.this, ObjectInfoDetailActivity.class);
                ObjectInfo lost = data.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("objectInfo", lost);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        found_menu_fab.setOnClickListener(this);
        found_search_fab.setOnClickListener(this);
        found_main_fab.setOnClickListener(this);
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showSuccess(List<ObjectInfo> list) {
        if (list != null) {
            data = list;
            found_listView.setAdapter(new MyAdapter(list, this));
        }
        Toast.makeText(this, "获取成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, "获取失败:" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showRefershSuccess(List<ObjectInfo> list) {
        if (list != null) {
            data = list;
            found_listView.setAdapter(new MyAdapter(list, this));
            found_swipeRefresh.setRefreshing(false);
        }
        Toast.makeText(this, "刷新成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showRefreshError(String error) {
        found_swipeRefresh.setRefreshing(false);
        Toast.makeText(this, "刷新失败:" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.found_menu_fab:
                if (flag) {
                    startAnimator();
                } else {
                    closeAnimator();
                }
                break;
            case R.id.found_search_fab:
                Toast.makeText(this, "搜索失物", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(FoundActivity.this, SearchActivity.class));
                break;
            case R.id.found_main_fab:
                Toast.makeText(this, "主页", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(FoundActivity.this, IndexActivity.class));
                break;
            default:
                break;
        }
    }

    private void startAnimator() {
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator main = ObjectAnimator.ofFloat(found_main_fab, "translationY", 0f, -200f);
        ObjectAnimator search = ObjectAnimator.ofFloat(found_search_fab, "translationY", 0f, -400f);
        ObjectAnimator menu = ObjectAnimator.ofFloat(found_menu_fab, "rotation", 0f, -45f);
        set.playTogether(main, search, menu);
        set.setInterpolator(new BounceInterpolator());
        set.setDuration(500).start();
        flag = false;
    }

    private void closeAnimator() {
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator main = ObjectAnimator.ofFloat(found_main_fab, "translationY", -200f, 0f);
        ObjectAnimator search = ObjectAnimator.ofFloat(found_search_fab, "translationY", -400f, 0f);
        ObjectAnimator menu = ObjectAnimator.ofFloat(found_menu_fab, "rotation", -45f, 0f);
        set.playTogether(main, search, menu);
        set.setInterpolator(new BounceInterpolator());
        set.setDuration(500).start();
        flag = true;
    }
}
